﻿/*
 * Author:xmfdsh 11届网络部--邓世豪
 * DataTime：2014-6-9
 * Copyright © 2014 大学生勤工助学服务队 Powered by xmfdsh & 四小狼工作室.
 * 版权所有，违者必究
 */
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;

namespace QGZX.Helper
{
    public class GetCollectJson
    {
        #region 获取department 的json方法 + string GetJson(List<MODEL.qgoa_user> list)
        /// <summary>
        /// 获取department 的json方法
        /// </summary>
        /// <param name="list">满足某种条件的list user</param>
        /// <returns>返回json字符串</returns>
        /// 
        public static string GetJson(List<MODEL.qgoa_user> list)
        {
            string[,] day = new string[36, 2];
            day[0, 0] = "一1,";
            day[1, 0] = "一3,";
            day[2, 0] = "一5,";
            day[3, 0] = "一7,";
            day[4, 0] = "一9,";
            day[5, 0] = "一11";

            day[6, 0] = "二1,";
            day[7, 0] = "二3,";
            day[8, 0] = "二5,";
            day[9, 0] = "二7,";
            day[10, 0] = "二9,";
            day[11, 0] = "二11";

            day[12, 0] = "三1,";
            day[13, 0] = "三3,";
            day[14, 0] = "三5,";
            day[15, 0] = "三7,";
            day[16, 0] = "三9,";
            day[17, 0] = "三11";

            day[18, 0] = "四1,";
            day[19, 0] = "四3,";
            day[20, 0] = "四5,";
            day[21, 0] = "四7,";
            day[22, 0] = "四9,";
            day[23, 0] = "四11";

            day[24, 0] = "五1,";
            day[25, 0] = "五3,";
            day[26, 0] = "五5,";
            day[27, 0] = "五7,";
            day[28, 0] = "五9,";
            day[29, 0] = "五11";

            day[30, 0] = "六1,";
            day[31, 0] = "六3,";
            day[32, 0] = "六5,";
            day[33, 0] = "六7,";
            day[34, 0] = "六9,";
            day[35, 0] = "六11";




            for (int i = 0; i < list.Count; i++)
            {
                string json = list[i].Json;
                //判断json是否有问题
                if (json.Length < 30)
                {

                    continue;
                }
                List<JobInfo> jobInfoList = JsonConvert.DeserializeObject<List<JobInfo>>(json);
                //遍历，把内容存到day数组
                foreach (JobInfo jobInfo in jobInfoList)
                {
                    StringBuilder result = new StringBuilder();
                    string test = jobInfo.classname;
                    result.Append(jobInfo.day);
                    if (jobInfo.note.Length < 2)
                        jobInfo.note += ",";
                    result.Append(jobInfo.note.Substring(0, 2));
                    for (int j = 0; j < day.GetLength(0); j++)
                    {
                        if (result.ToString().Equals(day[j, 0]))
                            day[j, 1] += "0";
                    }

                }
                //把数组中的标记 0去掉，并追加姓名，ps:这是我的处理手段，你们可以有更加好的方法，望改进
                for (int n = 0; n < day.GetLength(0); n++)
                {
                    if (day[n, 1] != null && day[n, 1].Length != 0)
                    {
                        if (!(day[n, 1].Substring(day[n, 1].Length - 1, 1).Equals("0")))
                        {
                            day[n, 1] = day[n, 1].Replace("0", "");
                            day[n, 1] = day[n, 1] + list[i].Name + ",";

                        }
                        day[n, 1] = day[n, 1].Replace("0", "");
                    }
                    else
                    {

                        day[n, 1] += list[i].Name + ",";
                    }
                }


            }

            department a = new department();
            a.Mon1 = day[0, 1];
            a.Mon3 = day[1, 1];
            a.Mon5 = day[2, 1];
            a.Mon7 = day[3, 1];
            a.Mon9 = day[4, 1];
            a.Mon11 = day[5, 1];

            a.Tues1 = day[6, 1];
            a.Tues3 = day[7, 1];
            a.Tues5 = day[8, 1];
            a.Tues7 = day[9, 1];
            a.Tues9 = day[10, 1];
            a.Tues11 = day[11, 1];

            a.Wed1 = day[12, 1];
            a.Wed3 = day[13, 1];
            a.Wed5 = day[14, 1];
            a.Wed7 = day[15, 1];
            a.Wed9 = day[16, 1];
            a.Wed11 = day[17, 1];

            a.Thur1 = day[18, 1];
            a.Thur3 = day[19, 1];
            a.Thur5 = day[20, 1];
            a.Thur7 = day[21, 1];
            a.Thur9 = day[22, 1];
            a.Thur11 = day[23, 1];


            a.Fri1 = day[24, 1];
            a.Fri3 = day[25, 1];
            a.Fri5 = day[26, 1];
            a.Fri7 = day[27, 1];
            a.Fri9 = day[28, 1];
            a.Fri11 = day[29, 1];


            a.Sat1 = day[30, 1];
            a.Sat3 = day[31, 1];
            a.Sat5 = day[32, 1];
            a.Sat7 = day[33, 1];
            a.Sat9 = day[34, 1];
            a.Sat11 = day[35, 1];
            //把已经填好的 部门信息转化为json，并返回
            return ObjectToJson(a);
        } 
        #endregion

        #region 把对象转化为json字符串 + ObjectToJson(object obj)
        /// <summary>
        /// 把对象转化为json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson(object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            byte[] dataBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0, (int)stream.Length);
            return Encoding.UTF8.GetString(dataBytes);
        } 
        #endregion

        #region 个人课表字符串的类，根据华农宝JSON格式 + class JobInfo
        /// <summary>
        /// 个人课表字符串的类，根据华农宝JSON格式
        /// </summary>
        public class JobInfo
        {

            public string classname { get; set; }
            public string teacher { get; set; }
            public string day { get; set; }
            public string note { get; set; }
            public string strWeek { get; set; }
            public string endWeek { get; set; }
            public string dsz { get; set; }
            public string location { get; set; }

        } 
        #endregion
        public class department
        {
            public string name { get; set; }
            public string Mon1 { get; set; }
            public string Mon3 { get; set; }
            public string Mon5 { get; set; }
            public string Mon7 { get; set; }
            public string Mon9 { get; set; }
            public string Mon11 { get; set; }
            public string Tues1 { get; set; }
            public string Tues3 { get; set; }
            public string Tues5 { get; set; }
            public string Tues7 { get; set; }

            public string Tues9 { get; set; }
            public string Tues11 { get; set; }
            public string Wed1 { get; set; }
            public string Wed3 { get; set; }
            public string Wed5 { get; set; }
            public string Wed7 { get; set; }
            public string Wed9 { get; set; }
            public string Wed11 { get; set; }
            public string Thur1 { get; set; }
            public string Thur3 { get; set; }
            public string Thur5 { get; set; }
            public string Thur7 { get; set; }
            public string Thur9 { get; set; }

            public string Thur11 { get; set; }
            public string Fri1 { get; set; }
            public string Fri3 { get; set; }
            public string Fri5 { get; set; }
            public string Fri7 { get; set; }
            public string Fri9 { get; set; }
            public string Fri11 { get; set; }
            public string Sat1 { get; set; }
            public string Sat3 { get; set; }
            public string Sat5 { get; set; }
            public string Sat7 { get; set; }
            public string Sat9 { get; set; }
            public string Sat11 { get; set; }


        }
    }
}
